package com.suixun.controller.information;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.authority.controller.BaseManager;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.suixun.Constants;
import com.suixun.entity.ZxArticle;
import com.suixun.service.credits.PutGoodsService;
import com.suixun.service.information.ZxArticleService;
import com.suixun.util.ObjectUtil;

@Controller
@RequestMapping(value = "/article")
public class ArticleController extends BaseManager{
    
    @Resource
    private ZxArticleService zxArticleService;
    @Resource
    private PutGoodsService putGoodsService;

    @RequestMapping(value = "/listArticle")
    @ResponseBody
    public Object listArticle(Integer page, Integer size, Integer tagId, String orderBy) {
        
    	if(ObjectUtil.isNull(page, size)){
           return fail("参数错误");
        }
    	
    	PageInfo<ZxArticle> pageInfo = zxArticleService.listArticleByTag(page, size, tagId, orderBy);
    	if(CollectionUtils.isNotEmpty(pageInfo.getList())){
    		String imageUrl = Constants.getImageUrl();
    		for(ZxArticle art : pageInfo.getList()){
    			if(StringUtil.isNotEmpty(art.getLitimg())){
    				art.setLitimg(imageUrl + art.getLitimg());
    			}
    		}
    	}
    	return success(pageInfo);
        
    }

    @RequestMapping(value = "/getArticle")
    @ResponseBody
    public Object getArticle(Integer articleId) {

        if(ObjectUtil.isNull(articleId)){
            return fail("参数错误");
        }
        
        ZxArticle art = zxArticleService.getArticle(articleId);
        if (art == null) {
        	return fail("该文章不存在或已被删除");
        }
        if(StringUtil.isNotEmpty(art.getLitimg())){
        	art.setLitimg(Constants.getImageUrl() + art.getLitimg());
        }
        
        return success(art);
    }
    
    
    @RequestMapping(value = "/updateArticle", method=RequestMethod.POST)
    @ResponseBody
    public Object updateArticle(Integer id, String title, String litimg, String content, Short del, Integer up, Short isshow, String goodsNumbers){
    	
    	if(ObjectUtil.isNull(id)){
    		return fail("参数错误");
    	}
    	try{
    		ZxArticle art = new ZxArticle();
    		art.setId(id);
    		if(StringUtil.isNotEmpty(title)){
    			art.setTitle(title);
    		}
    		if(StringUtil.isNotEmpty(content)){
    			art.setContent(content);
    		}
    		if(StringUtil.isNotEmpty(litimg)){
    			art.setLitimg(litimg);
    		}
    		if(up != null){
    			art.setUp(up);
    		}
    		if(del != null){
    			art.setDel(del);
    		}
    		if(isshow != null){
    			art.setIsshow(isshow);
    		}
    		art.setCreateTime(new Date());
    		zxArticleService.updateArticle(art, goodsNumbers);
    	}catch(Exception e){
    		return fail(e.getMessage());
    	}
    	return success();
    }
    /*@RequestMapping(value = "/updateArticle", method=RequestMethod.POST)
    @ResponseBody
    public Object updateArticle(Integer id, String title, String litimg, String content, Short del, Integer up, Short isshow){
    	
    	if(ObjectUtil.isNull(id)){
    		return fail("参数错误");
    	}
    	try{
    		ZxArticle art = new ZxArticle();
    		art.setId(id);
    		if(StringUtil.isNotEmpty(title)){
    			art.setTitle(title);
    		}
    		if(StringUtil.isNotEmpty(content)){
    			art.setContent(content);
    		}
    		if(StringUtil.isNotEmpty(litimg)){
    			art.setLitimg(litimg);
    		}
    		if(up != null){
    			art.setUp(up);
    		}
    		if(del != null){
    			art.setDel(del);
    		}
    		if(isshow != null){
    			art.setIsshow(isshow);
    		}
    		art.setCreateTime(new Date());
    		zxArticleService.updateArticle(art);
    	}catch(Exception e){
    		return fail(e.getMessage());
    	}
    	return success();
    }*/
    
    @RequestMapping(value = "/addArticle", method=RequestMethod.POST)
    @ResponseBody
    public Object addArticle(String title, String litimg, String content, Integer userId, Integer tagId, String goodsNumbers){
    	
    	if(ObjectUtil.isNull(title, content, tagId, userId)){
    		return fail("参数错误");
    	}
    	try{
    		ZxArticle art = new ZxArticle();
    		art.setTitle(title);
    		art.setContent(content);
    		art.setLitimg(litimg);
    		art.setUserId(userId);
    		return success(zxArticleService.addArticle(art, tagId, goodsNumbers));
    	}catch(Exception e){
    		return fail(e.getMessage());
    	}
    	
    }
    
    /*@RequestMapping(value = "/addArticle", method=RequestMethod.POST)
    @ResponseBody
    public Object addArticle(String title, String litimg, String content, Integer userId, Integer tagId){
    	
    	if(ObjectUtil.isNull(title, content, tagId, userId)){
    		return fail("参数错误");
    	}
    	try{
    		ZxArticle art = new ZxArticle();
    		art.setTitle(title);
    		art.setContent(content);
    		art.setLitimg(litimg);
    		art.setUserId(userId);
    		return success(zxArticleService.addArticle(art, tagId));
    	}catch(Exception e){
    		return fail(e.getMessage());
    	}
    	
    }*/
    
    @RequestMapping(value = "/setReadCount")
    @ResponseBody
    public Object setReadCount(Integer articleId, Integer count){
    	
    	if(ObjectUtil.isNull(articleId, count)){
    		return fail("参数错误");
    	}
    	try{
    		zxArticleService.setReadCount(articleId, count);
    	}catch(Exception e){
    		return fail(e.getMessage());
    	}
    	return success();
    }
    
    @RequestMapping(value = "/setZanCount")
    @ResponseBody
    public Object setZanCount(Integer articleId, Integer count){
    	
    	if(ObjectUtil.isNull(articleId, count)){
    		return fail("参数错误");
    	}
    	try{
    		zxArticleService.setZanCount(articleId, count);
    	}catch(Exception e){
    		return fail(e.getMessage());
    	}
    	return success();
    }
    
    @RequestMapping(value = "/setUp")
    @ResponseBody
    public Object setUp(Integer articleId){
    	if(ObjectUtil.isNull(articleId)){
    		return fail("参数错误");
    	}
    	
    	try{
    		zxArticleService.setUp(articleId);
    	}catch(Exception e){
    		return fail(e.getMessage());
    	}
    	
    	return success();
    }
    
    @RequestMapping(value = "/changeTag")
    @ResponseBody
    public Object changeTag(Integer articleId, Integer tagId){
    	if(ObjectUtil.isNull(articleId, tagId)){
    		return fail("参数错误");
    	}
    	
    	try{
    		zxArticleService.changeTag(articleId, tagId);
    	}catch(Exception e){
    		return fail(e.getMessage());
    	}
    	
    	return success();
    }
    
    @RequestMapping(value = "/getArticleCommentPage")
    @ResponseBody
    public Object getArticleCommentPage(Integer page, Integer size, Integer articleId, String content, String userName){
    	if(ObjectUtil.isNull(page, size)){
    		return fail("参数错误");
    	}
    	return success(zxArticleService.getArticleCommentPage(page, size, articleId, content, userName));
    }
    
    @RequestMapping(value = "/deleteArticleComments")
    @ResponseBody
    public Object deleteArticleComments(String commentIds){
    	
    	try{
    		zxArticleService.deleteArticleComments(commentIds);
    	}catch(Exception e){
    		return fail(e.getMessage());
    	}
    	
    	return success();
    }
    
    /**
     * <p>Title: getGoods</p>
     * <p> 功能描述: 获取资讯文章下的商品</p>
     * @param articleId
     * @return
     */
    @ResponseBody
	@RequestMapping(value = "/getGoods.json")
	public Object getGoods(Integer articleId) {
		try{
			return success(putGoodsService.getArticleGoodsArchives(articleId));
		}catch(Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return fail("操作异常");
		}
	}
}
